import { wechatrequest } from './API/request'
import { getStorageSync } from '../crossplatform/apiservice/storage'

/**
 * 查询我管理的职位
 * */
export const queryPostMessage = ({ type = '0' }) => {
  return wechatrequest.postparams(`@@/post/myPostList`, {
    data: {
      limit: 99999,
      type
    }
  })
}

/**
 * 职位删除
 * */
export const updatePostState = ({ state, id }) => {
  return wechatrequest.postparams(`/post/updateState`, {
    data: {
      state,
      id
    }
  })
}

/**
 * 岗位发布
 */
export const releasePostRequest = ({
  id,
  welfare,
  companyId,
  userId,
  category,
  education = 0,
  name,
  practiceLabel = 0,
  salary,
  settlementMethod,
  partUnitValue,
  type,
  workContent,
  // workHours,
  workPlace,
  isFast
}) => {
  let params = {}
  if (type == 0) {
    params = {
      isFast,
      companyId,
      userId,
      basicInfoModel: {
        name,
        type,
        salary,
        welfare,
        category,
        // workHours,
        workContent,
        settlementMethod,
        practiceLabel: 0,
        education: 0,
        unit: partUnitValue
      },
      postWorkLocations: workPlace
    }
  } else {
    params = {
      isFast,
      companyId,
      userId,
      basicInfoModel: {
        name,
        type,
        salary,
        welfare,
        category,
        education,
        // workHours,
        workContent,
        practiceLabel,
        settlementMethod,
        unit: partUnitValue
      },
      postWorkLocations: workPlace
    }
  }
  let url = ''
  if (id) {
    // 编辑
    const userinfo = getStorageSync('userinfo')
    params.userId = userinfo && userinfo.userId
    params = {
      category,
      education,
      name,
      postId: id,
      practiceLabel,
      salary,
      settlementMethod,
      type,
      unit: partUnitValue,
      workContent
      // workHours
    }
    url = `@@/post/updatePost`
    return wechatrequest.postbody(url, {
      data: {
        ...params
      }
    })
  } else {
    // 添加
    url = '/post/publishPost'
    return wechatrequest.post(url, {
      data: {
        ...params
      }
    })
  }
}

/**
 * 岗位列表查询
 */
export const getListRequest = ({ page = 1, pageSize = 10, postSearchCondition }) => {
  const landmark = getStorageSync('landmark')
  postSearchCondition.city = landmark.landmarkName || '上海'
  // 计算距离
  if (landmark.lng && landmark.lat) {
    postSearchCondition.latitude = landmark.lat
    postSearchCondition.longitude = landmark.lng
  }
  return wechatrequest.post(`/post/list?page=${page}&pageSize=${pageSize}`, {
    data: {
      ...postSearchCondition
    }
  })
}
/**
 * 岗位分享
 */
export const getShareRequest = ({ id }) => {
  return wechatrequest.postparams(`/member/postShare`, {
    data: {
      id
    }
  })
}

/**
 * 岗位刷新
 */
export const setTopRequest = ({ id }) => {
  return wechatrequest.postparams(`/post/setTop`, {
    data: {
      postId: id
    }
  })
}

/**
 * 岗位推荐列表
 */
export const jobRecommendRequest = ({ city }) => {
  return wechatrequest.get(`/post/recommend`, {
    data: {
      city
    }
  })
}

/**
 * 岗位详情
 */
export const jobDetailRequest = data => {
  const userinfo = getStorageSync('userinfo') || {}
  const landmark = getStorageSync('landmark')
  const userId = userinfo.userId
  return wechatrequest.postparams(`/post/getPostDetail`, {
    data: { ...data, userId, longitude: landmark.lng, latitude: landmark.lat }
  })
}

/**
 * 提交上架申请
 */
export const submitPutawayApply = ({ id }) => {
  return wechatrequest.postparams(`/post/submitPutawayApply`, {
    data: { id }
  })
}

/**
 * 获取拉黑企业的id列表
 */
export const getBlackCompanyIds = () => {
  return wechatrequest.postparams(`/post/getBlackCompanyIds`, {
    data: {}
  })
}

/**
 * 岗位当前状态 发布岗位后 IM消息是否发送成功
 */
export const postMessageStateRequest = ({ postId }) => {
  return wechatrequest.postparams(`@@/post/postMessageState`, {
    data: { postId }
  })
}

/**
 * 岗位id删除岗位消息 前端删除消息成功后 通知后端删除成功
 */
export const postMessageDeletedByPostIdRequest = ({ postId }) => {
  return wechatrequest.postparams(`@@/post/postMessageDeletedByPostId`, {
    data: { postId }
  })
}

/**
 * 未删除岗位消息会话 查询需要删除的IM会话
 */
export const notDeletedPostSessionRequest = ({ userId }) => {
  return wechatrequest.postparams(`@@/post/notDeletedPostSession`, {
    data: { userId }
  })
}

/**
 * 用户id删除岗位消息 删除完成后
 */
export const postMessageDeletedByUserIdRequest = ({ userId }) => {
  return wechatrequest.postparams(`@@/post/postMessageDeletedByUserId`, {
    data: { userId }
  })
}
